package github.leavesczy.reactivehttpsamples.util;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import github.leavesczy.reactivehttpsamples.BuildConfig;


public class Logger {

    private final static String TAG = "Logger";

    private static boolean LOG_ENABLED = BuildConfig.DEBUG;

    public static boolean isLoggable(int level) {
        return Log.isLoggable(TAG, level);
    }

    public static String getStackTraceString(Throwable th) {
        return Log.getStackTraceString(th);
    }

    public static void printStackTrace(String description, Throwable th) {
        if (LOG_ENABLED)
            Log.e(TAG, description + "\n" + getStackTraceString(th));
    }

    public static void v(String msg) {
        if (LOG_ENABLED)
            Log.v(TAG, msg);
    }

    public static void v(String msg, Throwable th) {
        if (LOG_ENABLED)
            Log.v(TAG, msg, th);
    }

    public static void d(String msg) {
        if (LOG_ENABLED)
            Log.d(TAG, msg);
    }

    public static void d(String msg, Throwable th) {
        if (LOG_ENABLED)
            Log.d(TAG, msg, th);
    }

    public static void d(String tag,String msg) {
        if (LOG_ENABLED)
            Log.d(tag, msg);
    }

    public static void i(String msg) {
        if (LOG_ENABLED)
            Log.i(TAG, msg);
    }
    public static void i(String tag,String msg) {
        if (LOG_ENABLED)
            Log.i(tag, msg);
    }
    public static void i(String msg, Throwable th) {
        if (LOG_ENABLED)
            Log.i(TAG, msg, th);
    }

    public static void w(String msg) {
        if (LOG_ENABLED)
            Log.w(TAG, msg);
    }

    public static void w(String msg, Throwable th) {
        if (LOG_ENABLED)
            Log.w(TAG, msg, th);
    }

    public static void w(Throwable th) {
        if (LOG_ENABLED)
            Log.w(TAG, th);
    }

    public static void e(String msg) {
        if (LOG_ENABLED)
            Log.e(TAG, msg);
    }

    // 打印对象
    public static void e(String tag, String msg, Object object) {

        if (LOG_ENABLED && object != null) {
            Gson gson = new Gson();
            String s = gson.toJson(object);
            if (!TextUtils.isEmpty(s)) {
                Log.e(tag, msg + ":" + s);
            }
        }

    }

    public static void e(String tag, String msg) {
        if (LOG_ENABLED) {
            Log.e(tag, msg);
        }
    }

    public static void e(String msg, Throwable th) {
        if (LOG_ENABLED)
            Log.e(TAG, msg, th);
    }

    public static void showLogCompletion(String log, int showCount) {
        if (log.length() > showCount) {
            String show = log.substring(0, showCount);
            Log.i(TAG, show + "");
            if ((log.length() - showCount) > showCount) {//剩下的文本还是大于规定长度
                String partLog = log.substring(showCount, log.length());
                showLogCompletion(partLog, showCount);
            } else {
                String surplusLog = log.substring(showCount, log.length());
                Log.i(TAG, surplusLog + "");
            }

        } else {
            Log.i(TAG, log + "");
        }
    }
}
